package advertising

import (
	"context"
	"device-admin/internal/app/grpc/client"
	"device-admin/internal/app/grpc/protoc/advertisingc"
	"fmt"

	"192.168.1.75/go-pkg/errorx"
)

type Material struct{}

// 素材列表
func (Material) MaterialList(ctx context.Context, params *advertisingc.MaterialListReq) (
	data []*advertisingc.MaterialListRsp_Data_MaterialList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.MaterialList(ctx, params)
	if rspErr != nil {
		return data, count, errorx.New(fmt.Sprintf("查询广告素材列表错误: %s", rspErr.Error()), -1)
	}

	if rsp == nil {
		return data, count, errorx.New("广告系统返回空", -1)
	}

	if rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}

	if rsp.Data.List == nil {
		rsp.Data.List = []*advertisingc.MaterialListRsp_Data_MaterialList{}
	}
	return rsp.Data.List, rsp.Data.Count, nil
}

// 素材审核
func (Material) MaterialAudit(ctx context.Context, params *advertisingc.MaterialAuditReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.MaterialAudit(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return errorx.New(rsp.Msg, int(rsp.Code))
	}

	return
}

// 素材禁用启用
func (Material) MaterialSwitch(ctx context.Context, params *advertisingc.MaterialSwitchReq) (err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.MaterialSwitch(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return errorx.New(rsp.Msg, int(rsp.Code))
	}

	return
}

// 素材审核记录
func (Material) MaterialAuditLogList(ctx context.Context, params *advertisingc.MaterialAuditLogListReq) (
	data []*advertisingc.MaterialAuditLogListRsp_Data_MaterialAuditLogList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.MaterialAuditLogList(ctx, params)
	if rspErr != nil {
		return data, count, errorx.New(fmt.Sprintf("查询广告素材审核记录列表错误: %s", rspErr.Error()), -1)
	}

	if rsp == nil {
		return data, count, errorx.New("广告系统返回空", -1)
	}

	if rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}

	if rsp.Data.List == nil {
		rsp.Data.List = []*advertisingc.MaterialAuditLogListRsp_Data_MaterialAuditLogList{}
	}
	return rsp.Data.List, rsp.Data.Count, nil
}

// 素材文件夹列表
func (Material) MaterialFolderList(ctx context.Context, params *advertisingc.MaterialFolderListReq) (
	data []*advertisingc.MaterialFolderListRsp_FolderList, count int64, err error) {
	cli, connErr := client.GetAdvertisingClient()
	if connErr != nil {
		err = errorx.New("广告系统连接错误", -1)
		return
	}
	rsp, rspErr := cli.MaterialFolderList(ctx, params)
	if rspErr != nil || rsp.Code != 0 {
		return data, count, errorx.New(rsp.Msg, int(rsp.Code))
	}
	if rsp.List == nil {
		rsp.List = []*advertisingc.MaterialFolderListRsp_FolderList{}
	}
	return rsp.List, rsp.Total, nil
}
